import { apiAcceptOrder } from '../../other/api'

const { regeneratorRuntime, throttle, dayjs } = global

Page(global.connect(
  state => ({
    ...state.globalReducer,
    ...state.homeReducer,
    ...state.activityReducer
  }),
  dispatch => ({
    showSidebar(data) {
      dispatch(global.globalReducer.showSidebar(data))
    },
    hideSidebar(data) {
      dispatch(global.globalReducer.hideSidebar(data))
    },
    // 待接单列表
    async getWorkerOrderingList(refresh) {
      try {
        console.log('getWorkerOrderingList 加载列表1', refresh)
        if (!this.uiLoading1) {
          // step1
          this.setData({ uiLoading1: true })

          // step2
          const { uiCurrentPage1, uiPageSize1, uiKeyword1, uiList1, uiStartDate1 } = this.data
          const nextPage = refresh ? 1 : uiCurrentPage1 + 1
          const condition = {
            id: this.data.minaLoginRes.data.id,
            time: uiStartDate1,
            page: nextPage,
            size: uiPageSize1,
            keyword: uiKeyword1,
            type: 1
          }
          const actionRes = await dispatch(global.workerReducer.getOrderList(condition))
          if (actionRes.actionStatus === 'SUCCESS') {
            // ...
          } else {
            this.selectComponent('#toast').handleShow({
              message: actionRes.apiMes
            })
            console.error(actionRes)
          }
          this.setData({
            uiCurrentPage1: nextPage,
            uiList1: refresh ? actionRes.data : [...uiList1, ...actionRes.data]
          })
        }
      } catch (error) {
        console.error(error)
      }
      this.setData({ uiLoading1: false })
    },
    // 已接单列表
    async getWorkerOrderedList(refresh) {
      try {
        console.log('getWorkerOrderedList')
        if (!this.uiLoading2) {
          // step1
          this.setData({ uiLoading2: true })

          // step2
          const { uiCurrentPage2, uiPageSize2, uiKeyword2, uiList2, uiStartDate2 } = this.data
          const nextPage = refresh ? 1 : uiCurrentPage2 + 1
          const condition = {
            id: this.data.minaLoginRes.data.id,
            time: uiStartDate2,
            page: nextPage,
            size: uiPageSize2,
            keyword: uiKeyword2,
            type: 2
          }
          const actionRes = await dispatch(global.workerReducer.getOrderList(condition))
          console.log(actionRes)
          if (actionRes.actionStatus === 'SUCCESS') {
            // ...
          } else {
            this.selectComponent('#toast').handleShow({
              message: actionRes.apiMes
            })
            console.error(actionRes)
          }
          this.setData({
            uiCurrentPage2: nextPage,
            uiList2: refresh ? actionRes.data : [...uiList2, ...actionRes.data]
          })
          // step3
        }
      } catch (error) {
        console.error(error)
      }
      this.setData({ uiLoading2: false })
    },
  })
)({
  data: {
    uiTabIndex: 1,

    uiFnLock1: false,
    uiLoading1: false,
    uiList1: [],
    uiCurrentPage1: 0,
    uiPageSize1: 5,
    uiKeyword1: '',
    uiShowSearch1: false,
    uiStartDate1: dayjs().format('YYYY-MM-DD'),


    uiFnLock2: false,
    uiLoading2: false,
    uiList2: [],
    uiCurrentPage2: 0,
    uiPageSize2: 5,
    uiKeyword2: '',
    uiShowSearch2: false,
    uiStartDate2: dayjs().format('YYYY-MM-DD'),
  },
  onLoad() {
    console.log('page worker index onLoad')
  },
  onShow() {
    // 加载list1 todo
    global.waitUntilLogin(() => this.getWorkerOrderingList(true))
    // 加载list2
    global.waitUntilLogin(() => this.getWorkerOrderedList(true))
  },
  handleLicon1Tap(e) {
    console.log('handleLicon1Tap', e)
    this.showSidebar()
  },
  handleRicon1Tap(e) {
    const { uiTabIndex } = this.data
    if (uiTabIndex === 0) {
      const uiShowSearch1 = !this.data.uiShowSearch1
      this.setData({
        uiShowSearch1
      })
    }
    if (uiTabIndex === 1) {
      const uiShowSearch2 = !this.data.uiShowSearch2
      this.setData({
        uiShowSearch2
      })
    }
  },
  handleTab(e) {
    const uiTabIndex = e.currentTarget.dataset.index
    this.setData({ uiTabIndex })
    if (uiTabIndex === 0) {
      this.getWorkerOrderingList(true)
    }
    if (uiTabIndex === 1) {
      this.getWorkerOrderedList(true)
    }
  },
  loadList1() {
    this.getWorkerOrderingList()
  },
  loadList2() {
    this.getWorkerOrderedList()
  },
  handleSearchConfirm1(e) {
    console.log('handleSearchConfirm1', e)
    const keyword = e.detail.detail.value
    this.setData({ uiKeyword1: keyword })
    this.getWorkerOrderingList(true)
  },
  handleSearchConfirm2(e) {
    console.log('handleSearchConfirm2', e)
    const keyword = e.detail.detail.value
    this.setData({ uiKeyword2: keyword })
    this.getWorkerOrderedList(true)
  },
  handleSearchConfirm(e) {
    console.log('handleSearchConfirm', e)
    const uiKeyword = e.detail.detail.value
    this.setData({ uiKeyword })
    this.handleReloadListData(true)
  },
  handleItemTap(e) {
    console.log('handleItemTap', e)
    const { activity_id, order_no } = e.currentTarget.dataset.detail
    global.handleRoute({
      route: 'order-detail',
      query: {
        activity_id,
        order_no
      }
    })
  },
  handleBtn1(e) {
    console.log('handleBtn1', e)
  },
  handleBtn2(e) {
    console.log('handleBtn2', e)
    const { detail } = e.currentTarget.dataset
    const { order_no } = detail
    console.log(order_no)
    apiAcceptOrder({ order_no }).then(res => {
      console.log(res)
      if (res.apiStatus === 'SUCCESS') {
        this.selectComponent('#toast').handleShow({
          type: 'success',
          message: res.apiMes || '接受成功'
        })
        this.getWorkerOrderingList(true)
      } else {
        this.selectComponent('#toast').handleShow({
          message: res.apiMes || '接受订单失败，请稍后再试'
        })
      }
    }).catch(err => {
      console.error(err)
      this.selectComponent('#toast').handleShow({
        message: err && err.apiMes || '接受订单失败，请稍后再试'
      })
    })
  },
  // 日历1
  handleOpenCalendar1(e) {
    console.log('handleOpenCalendar1', e)
    this.selectComponent('#calendar1').handleShow(null, {
      title: '',
      range: false,
      startDate: this.data.uiStartDate1
    })
  },
  handleCalendar1Hide(e) {
    console.log('handleCalendar1Hide', e)
    this.selectComponent('#calendar1').handleHide(false)
  },
  handleCalendar1Confirm(e) {
    console.log('handleCalendar1Confirm', e)
    this.selectComponent('#calendar1').handleHide(false)
    this.setData({ uiStartDate1: e.detail.data.startDate })
    this.getWorkerOrderingList(true)
  },
  // 日历2
  handleOpenCalendar2(e) {
    console.log('handleOpenCalendar2', e)
    this.selectComponent('#calendar2').handleShow(null, {
      title: '',
      range: false,
      startDate: this.data.uiStartDate2
    })
  },
  handleCalendar2Hide(e) {
    console.log('handleCalendar2Hide', e)
    this.selectComponent('#calendar2').handleHide(false)
  },
  handleCalendar2Confirm(e) {
    console.log('handleCalendar2Confirm', e)
    this.selectComponent('#calendar2').handleHide(false)
    this.setData({ uiStartDate2: e.detail.data.startDate })
    this.getWorkerOrderedList(true)
  },
}))